package ${tablePath}.service.impl;

import ${tablePath}.dao.${table.className}Dao;
import ${tablePath}.entity.${table.className};
import ${tablePath}.service.${table.className}Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import annotation.io.gitee.xiaouyudeguang.common.ApiService;
import exception.io.gitee.xiaouyudeguang.common.BusinessException;
import request.io.gitee.xiaouyudeguang.common.RequestDto;
import impl.service.io.gitee.xiaouyudeguang.common.AbstractServiceImpl;
import utils.io.gitee.xiaouyudeguang.common.IdUtils;
import utils.io.gitee.xiaouyudeguang.common.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

 /**
  * ${table.comment}ServiceImpl
  * @author ${author}
  * @since ${dateTime}
  */
@ApiService(dataScope = false)
public class ${table.className}ServiceImpl extends AbstractServiceImpl<${table.className}Dao, ${table.className}> implements ${table.className}Service {

    @Override
    public ${table.className} getById(String tenantId, String appId, String id) throws Exception {
        return StringUtils.isBlank(id) ? null : getOne(lambdaWrapper().eq(${table.className}::getAppId, appId).eq(${table.className}::get${table.entityId}Id, id));
    }

    @Override
    public List<${table.className}> getByIds(String tenantId, String appId, Set<String> ids) throws Exception {
       return list(lambdaWrapper().eq(${table.className}::getAppId, appId).in(${table.className}::get${table.entityId}Id, ids));
    }

    @Override
    public ${table.className} getOne(String tenantId, String appId, SFunction<${table.className}, ?> function, Object value) {
        return getOne(lambdaWrapper().eq(${table.className}::getAppId, appId).eq(function, value));
    }

    @Override
    public Boolean deleteById(String tenantId, String appId, String id) throws Exception {
        return remove(lambdaWrapper().eq(${table.className}::getAppId, appId).eq(${table.className}::get${table.entityId}Id, id));
    }

    @Override
    public Boolean deleteByIds(String tenantId, String appId, Set<String> ids) throws Exception {
        return remove(lambdaWrapper().eq(${table.className}::getAppId, appId).in(${table.className}::get${table.entityId}Id, ids));
    }

    @Override
    public ${table.className} updateById(String tenantId, String appId, RequestDto<${table.className}> requestDto) throws Exception {
        ${table.className} data = requestDto.getData();
        ${table.className} ${lowerClassName} = getById(tenantId, appId, data.get${table.entityId}Id());
        if (${lowerClassName} == null) {
            throw new BusinessException("数据不存在");
        }
        data.setId(${lowerClassName}.getId());
        return super.updateById(tenantId, appId, data.getRequest());
    }

    @Override
    public List<${table.className}> updateByIds(String tenantId, String appId, RequestDto<${table.className}> requestDto) throws Exception {
        ${table.className} data = requestDto.getData();
        List<${table.className}> dataList = getByIds(tenantId, appId, requestDto.getIds());
        if (isNotEmpty(dataList)) {
            for (${table.className} it: dataList) {
                data.set${table.entityId}Id(it.get${table.entityId}Id());
                updateById(tenantId, appId, data.getRequest());
            }
        }
        return dataList;
    }

    @Override
    public ${table.className} save(String tenantId, String appId, RequestDto<${table.className}> requestDto) throws Exception {
        ${table.className} data = requestDto.getData();
        data.setAppId(appId);
        data.setTenantId(tenantId);
        data.setCustomId(tenantId);
        data.set${table.entityId}Id(IdUtils.randomUUID());
        return super.save(tenantId, appId, data.getRequest());
    }

    @Override
    public ${table.className} modify(String tenantId, String appId, RequestDto<${table.className}> requestDto) throws Exception {
        ${table.className} data = requestDto.getData();
        ${table.className} ${lowerClassName} = getById(tenantId, appId, data.get${table.entityId}Id());
        if (${lowerClassName} == null) {
            throw new BusinessException("数据不存在");
        }
        data.setId(${lowerClassName}.getId());
        return super.modify(tenantId, appId, data.getRequest());
    }

    @Override
    public List<${table.className}> list(String tenantId, String appId, RequestDto<${table.className}> requestDto) throws Exception {
        ${table.className} data = requestDto.getData();
        LambdaQueryWrapper<${table.className}> lambdaQueryWrapper = lambdaWrapper().orderByDesc(${table.className}::getCreated);
        lambdaQueryWrapper.eq(${table.className}::getAppId, appId);
        if (StringUtils.isNotBlank(requestDto.getStartTime())) {
            lambdaQueryWrapper.gt(${table.className}::getCreated, requestDto.getStartTime());
        }
        if (StringUtils.isNotBlank(requestDto.getEndTime())) {
            lambdaQueryWrapper.lt(${table.className}::getCreated, requestDto.getEndTime());
        }
        return list(lambdaQueryWrapper);
    }

    @Override
    public List<${table.className}> list(String tenantId, String appId, SFunction<${table.className}, ?> function, Object value) {
        return list(lambdaWrapper().eq(${table.className}::getAppId, appId).eq(function, value));
    }

    @Override
    public PageImpl<${table.className}> page(String tenantId, String appId, RequestDto<${table.className}> requestDto) throws Exception {
        ${table.className} data = requestDto.getData();
        LambdaQueryWrapper<${table.className}> lambdaQueryWrapper = lambdaWrapper().orderByDesc(${table.className}::getCreated);
        lambdaQueryWrapper.eq(${table.className}::getAppId, appId);
        if (StringUtils.isNotBlank(requestDto.getStartTime())) {
            lambdaQueryWrapper.gt(${table.className}::getCreated, requestDto.getStartTime());
        }
        if (StringUtils.isNotBlank(requestDto.getEndTime())) {
            lambdaQueryWrapper.lt(${table.className}::getCreated, requestDto.getEndTime());
        }
        IPage<${table.className}> page = page(Page.of(requestDto.getCurrent(), requestDto.getSize()), lambdaQueryWrapper);
        List<${table.className}> recordList = page.getRecords();
        if (recordList == null || recordList.isEmpty()) {
            return emptyPage();
        }
        return page(recordList, page.getTotal());
    }
}
